package org.strongme.modules.project.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class WordDocGenerator {
	private static Configuration configuration = null;
	private static Map<String, Template> allTemplates = null;
	
	static {
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassForTemplateLoading(WordDocGenerator.class, "/");
		allTemplates = new HashMap<>();	// Java 7 钻石语法
		try {
			allTemplates.put("report_main", configuration.getTemplate("report_main_tpl.ftl"));
			allTemplates.put("report_vice", configuration.getTemplate("report_vice_tpl.ftl"));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	private WordDocGenerator() {
		throw new AssertionError();
	}

	public static void createDoc(Map<?, ?> dataMap, String type, OutputStream outStream) {
		Template t = allTemplates.get(type);
		try {
			t.process(dataMap, new OutputStreamWriter(outStream));
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
	
}
